(function() {
  jQuery(function() {
    var dialog, edit_notify_show, notify_show;
    dialog = $("#role_user_create");
    notify_show = function(message) {
      $(".notify", dialog).html(message);
      return $(".notify", dialog).show();
    };
    dialog.delegate("form", "submit", function(e) {
      var name;
      e.preventDefault();
      name = $("input.name", dialog).val();
      if ($.isEmptyObject(name)) {
        notify_show("名称不能为空！");
        return;
      }
      return $.ajax({
        url: "/admins/groups",
        type: "POST",
        data: $("form", dialog).serialize(),
        success: function(html) {
          $(".user_role table tr.no_data").remove();
          $(".user_role table>tbody").prepend(html);
          dialog.modal('hide');
          return $("form", dialog)[0].reset();
        },
        error: function(data) {
          var error;
          try {
            return notify_show(JSON.parse(data.responseText).join('<br/>'));
          } catch (_error) {
            error = _error;
            return notify_show(data.responseText);
          }
        }
      });
    }).on("click", "button.save", function() {
      return $("form", dialog).submit();
    });
    $(".user_role").on("click", "table>tbody>tr", function(e) {
      var url;
      url = $(e.currentTarget).attr("data-show-url");
      return $.ajax({
        url: url,
        type: "get",
        success: function(html) {
          return $(".user_permission table>tbody").html(html);
        }
      });
    }).on("click", "table>tbody .edit", function(e) {
      var url;
      e.preventDefault();
      url = $(e.currentTarget).attr("href");
      return $.ajax({
        url: url,
        dataType: "script"
      });
    });
    edit_notify_show = function(message, dialog) {
      $(".notify", dialog).html(message);
      return $(".notify", dialog).show();
    };
    return $("body").on("submit", ".user_group_edit form", function(e) {
      var $form, edit_dialog, id, name, url;
      e.preventDefault();
      $form = $(e.currentTarget);
      url = $form.attr("action");
      id = $form.attr("data-value-id");
      edit_dialog = $("#" + id + "_user_group_edit");
      name = $("input.name", edit_dialog).val();
      if ($.isEmptyObject(name)) {
        edit_notify_show("名称不能为空！", edit_dialog);
        return;
      }
      return $.ajax({
        url: url,
        type: "PUT",
        data: $form.serialize(),
        success: function(data) {
          $(".user_role table td." + data.id + "_group_name").html(data.name);
          return edit_dialog.modal("hide");
        },
        error: function(data) {
          var error;
          console.log("....");
          try {
            return edit_notify_show(JSON.parse(data.responseText).join("<br />"), edit_dialog);
          } catch (_error) {
            error = _error;
            return edit_notify_show(data.responseText, edit_dialog);
          }
        }
      });
    }).on("click", ".user_group_edit button.save", function(e) {
      var $btn, edit_dialog, id;
      $btn = $(e.currentTarget);
      id = $btn.attr("data-value-id");
      edit_dialog = $("#" + id + "_user_group_edit");
      return $("form", edit_dialog).submit();
    });
  });

}).call(this);
